package com.xiaoze.springcloud.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoze.springcloud.dao.UserDao;
import com.xiaoze.springcloud.entity.CourseType;
import com.xiaoze.springcloud.entity.User;
import com.xiaoze.springcloud.mapper.CourseTypeMapper;
import com.xiaoze.springcloud.service.CourseTypeService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * CourseTypeServiceImpl
 *
 * @author xiaoze
 * @date 2018/6/3
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements CourseTypeService {

    @Autowired
    private UserDao userDao;

    @Override
    public void addCourseType(CourseType courseType) {

        baseMapper.insert(courseType);

    }

    @Override
    public void removeCourseType(Integer typeId) {

        baseMapper.deleteById(typeId);

    }

    @Override
    @GlobalTransactional(name = "create-user",rollbackFor = Exception.class)
    public void updateCourseType(CourseType courseType) {

        baseMapper.updateById(courseType);

        // 下面这些不用管，主要是做seata测试,若要测试可以放开
//        Integer userNo = (int)((Math.random()*9+1)*100000);
//        User user = new User();
//        user.setUserNo(userNo + "");
//        user.setUserPwd("123456");
//        user.setUserName("小" + userNo);
//        userDao.createOneUser(user);
//        System.out.println(1/0);

    }

    @Override
    public CourseType getCourseTypeById(Integer typeId) {

        return baseMapper.selectById(typeId);

    }

    @Override
    public List<CourseType> loadAll() {

        return baseMapper.selectList(null);

    }

}
